package org.chen.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.chen.domain.entity.BackgroundResource;
import org.chen.domain.response.Result;
import org.chen.service.BackgroundResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/background")
public class BackgroundResourceController {

    @Autowired
    private BackgroundResourceService backgroundResourceService;

    /**
     * 分页查询背景资源
     */
    @GetMapping("/list")
    public Result<IPage<BackgroundResource>> list(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(value = "type", required = false) String fileType,
            @RequestParam(value = "keyword", required = false) String keyword) {
        
        Page<BackgroundResource> pageParam = new Page<>(page, pageSize);
        IPage<BackgroundResource> result = backgroundResourceService.pageList(pageParam, fileType, keyword);
        
        return Result.success(result);
    }

    /**
     * 保存背景资源
     */
    @PostMapping("/save")
    public Result<Boolean> save(@RequestBody BackgroundResource backgroundResource) {
        boolean success = backgroundResourceService.saveBackground(backgroundResource);
        return success ? Result.success(true) : Result.error("保存背景资源失败");
    }

    /**
     * 删除背景资源
     */
    @DeleteMapping("/{id}")
    public Result<Boolean> delete(@PathVariable String id) {
        boolean success = backgroundResourceService.removeBackground(id);
        return success ? Result.success(true) : Result.error("删除背景资源失败");
    }

    /**
     * 获取背景资源详情
     */
    @GetMapping("/{id}")
    public Result<BackgroundResource> getInfo(@PathVariable String id) {
        BackgroundResource background = backgroundResourceService.getBackground(id);
        return background != null ? Result.success(background) : Result.error("背景资源不存在");
    }
}